home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Generators / C / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  13.5 KB  |  482 lines

  1. /// Includes
  2. #define INTUI_V36_NAMES_ONLY
  3.  
  4. #include <exec/nodes.h>                 // exec
  5. #include <exec/lists.h>
  6. #include <exec/types.h>
  7. #include <intuition/intuition.h>        // intuition
  8. #include <intuition/gadgetclass.h>
  9. #include <libraries/gadtools.h>         // libraries
  10. #include <libraries/reqtools.h>
  11. #include <libraries/asl.h>
  12. #include <clib/exec_protos.h>           // protos
  13. #include <clib/intuition_protos.h>
  14. #include <clib/reqtools_protos.h>
  15. #include <clib/asl_protos.h>
  16. #include <pragmas/exec_pragmas.h>       // pragmas
  17. #include <pragmas/intuition_pragmas.h>
  18. #include <pragmas/gadtools_pragmas.h>
  19. #include <pragmas/reqtools_pragmas.h>
  20. #include <pragmas/asl_pragmas.h>
  21.  
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26.  
  27. #include "DEV_IE:Generators/defs.h"
  28. #include "DEV_IE:Include/IEditor.h"
  29. #include "DEV_IE:Generators/C/Config.h"
  30. #include "DEV_IE:Generators/C/Protos.h"
  31. ///
  32. /// Prototypes
  33. static void     SavePrefs( void );
  34. static BOOL     GetFile( struct IE_Data *, STRPTR, STRPTR, STRPTR );
  35. ///
  36. /// Data
  37. static ULONG    CheckedTag[] = { GTCB_Checked, 0, TAG_END };
  38. static UBYTE    Back, MoreBack;
  39.  
  40. BOOL            PrefsOK = FALSE;
  41. static TEXT     PrefsFile[] = "PROGDIR:C.prefs";
  42.  
  43. struct Library *AslBase;
  44. ///
  45.  
  46.  
  47. /// Config
  48. void Config( __A0 struct IE_Data *IE )
  49. {
  50.     struct Window  *Wnd = NULL;
  51.     struct Gadget  *GList = NULL, *Gadgets[ Conf_CNT ];
  52.  
  53.     GrabOldPrefs( IE );
  54.  
  55.     if( OpenConfWindow( &Wnd, &GList, &Gadgets[0], IE )) {
  56.  
  57.     ( *IE->Functions->Status )( "Cannot open my window!", TRUE, 0 );
  58.  
  59.     } else {
  60.  
  61.     Back     = Prefs.Flags;
  62.     MoreBack = Prefs.MoreFlags;
  63.  
  64.     Prefs.Flags     = ~Prefs.Flags;
  65.     Prefs.MoreFlags = ~Prefs.MoreFlags;
  66.  
  67.     TemplateKeyPressed( Wnd, Gadgets, IE, NULL );
  68.     ClickKeyPressed( Wnd, Gadgets, IE, NULL );
  69.     MsgKeyPressed( Wnd, Gadgets, IE, NULL );
  70.     HandlerKeyPressed( Wnd, Gadgets, IE, NULL );
  71.     KeyHandlerKeyPressed( Wnd, Gadgets, IE, NULL );
  72.     ToLowerKeyPressed( Wnd, Gadgets, IE, NULL );
  73.     SmartStrKeyPressed( Wnd, Gadgets, IE, NULL );
  74.     NewTmpKeyPressed( Wnd, Gadgets, IE, NULL );
  75.     CatCompKeyPressed( Wnd, Gadgets, IE, NULL );
  76.     NoKPKeyPressed( Wnd, Gadgets, IE, NULL );
  77.  
  78.     Prefs.Flags     = Back;
  79.     Prefs.MoreFlags = MoreBack;
  80.  
  81.     GT_SetGadgetAttrs( Gadgets[ GD_Chip ], Wnd, NULL,
  82.                GTST_String, Prefs.ChipString, TAG_END );
  83.  
  84.     GT_SetGadgetAttrs( Gadgets[ GD_Headers ], Wnd, NULL,
  85.                GTST_String, Prefs.HeadersFile, TAG_END );
  86.  
  87.     GT_SetGadgetAttrs( Gadgets[ GD_Hook ], Wnd, NULL,
  88.                GTST_String, Prefs.HookDef, TAG_END );
  89.  
  90.     GT_SetGadgetAttrs( Gadgets[ GD_Reg ], Wnd, NULL,
  91.                GTST_String, Prefs.RegisterDef, TAG_END );
  92.  
  93.     do {
  94.         WaitPort( Wnd->UserPort );
  95.     } while( HandleConfIDCMP( Wnd, &Gadgets[0], IE ) == 0 );
  96.     }
  97.  
  98.     CloseWnd( &Wnd, &GList );
  99. }
  100.  
  101. BOOL MsgKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  102. {
  103.     /*  Routine when "Intui_Message "'s activation key is pressed  */
  104.  
  105.     CheckedTag[1] = ( Prefs.Flags & INTUIMSG ) ? FALSE : TRUE;
  106.     GT_SetGadgetAttrsA( Gadgets[ GD_Msg ], Wnd, NULL, (struct TagItem *)CheckedTag );
  107.  
  108.     /*  ...or return TRUE not to call the gadget function  */
  109.     return MsgClicked( Wnd, Gadgets, IE, Msg );
  110. }
  111.  
  112. BOOL ClickKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  113. {
  114.     /*  Routine when "Clicked _Ptr  "'s activation key is pressed  */
  115.  
  116.     CheckedTag[1] = ( Prefs.Flags & CLICKED ) ? FALSE : TRUE;
  117.     GT_SetGadgetAttrsA( Gadgets[ GD_Click ], Wnd,
  118.             NULL, (struct TagItem *)CheckedTag );
  119.  
  120.     /*  ...or return TRUE not to call the gadget function  */
  121.     return ClickClicked( Wnd, Gadgets, IE, Msg );
  122. }
  123.  
  124. BOOL UseKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  125. {
  126.     /*  Routine when "_Use"'s activation key is pressed  */
  127.  
  128.     /*  ...or return TRUE not to call the gadget function  */
  129.     return UseClicked( Wnd, Gadgets, IE, Msg );
  130. }
  131.  
  132. BOOL SaveKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  133. {
  134.     /*  Routine when "_Save"'s activation key is pressed  */
  135.  
  136.     /*  ...or return TRUE not to call the gadget function  */
  137.     return SaveClicked( Wnd, Gadgets, IE, Msg );
  138. }
  139.  
  140. BOOL CancKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  141. {
  142.     /*  Routine when "_Cancel"'s activation key is pressed  */
  143.  
  144.     /*  ...or return TRUE not to call the gadget function  */
  145.     return CancClicked( Wnd, Gadgets, IE, Msg );
  146. }
  147.  
  148. BOOL HandlerKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  149. {
  150.     /*  Routine when "IDCMP _Handler"'s activation key is pressed  */
  151.  
  152.     CheckedTag[1] = ( Prefs.Flags & IDCMP_HANDLER ) ? FALSE : TRUE;
  153.     GT_SetGadgetAttrsA( Gadgets[ GD_Handler ], Wnd,
  154.             NULL, (struct TagItem *)CheckedTag );
  155.  
  156.     /*  ...or return TRUE not to call the gadget function  */
  157.     return HandlerClicked( Wnd, Gadgets, IE, Msg );
  158. }
  159.  
  160. BOOL KeyHandlerKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  161. {
  162.     /*  Routine when "_Key Handler  "'s activation key is pressed  */
  163.  
  164.     CheckedTag[1] = ( Prefs.Flags & KEY_HANDLER ) ? FALSE : TRUE;
  165.     GT_SetGadgetAttrsA( Gadgets[ GD_KeyHandler ], Wnd,
  166.             NULL, (struct TagItem *)CheckedTag );
  167.  
  168.     /*  ...or return TRUE not to call the gadget function  */
  169.     return KeyHandlerClicked( Wnd, Gadgets, IE, Msg );
  170. }
  171.  
  172. BOOL TemplateKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  173. {
  174.     /*  Routine when "_Template     "'s activation key is pressed  */
  175.     CheckedTag[1] = ( Prefs.Flags & GEN_TEMPLATE ) ? FALSE : TRUE;
  176.     GT_SetGadgetAttrsA( Gadgets[ GD_Template ], Wnd,
  177.             NULL, (struct TagItem *)CheckedTag );
  178.  
  179.     /*  ...or return TRUE not to call the gadget function  */
  180.     return TemplateClicked( Wnd, Gadgets, IE, Msg );
  181. }
  182.  
  183. BOOL ToLowerKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  184. {
  185.     /*  Routine when "To Lo_wer     "'s activation key is pressed  */
  186.     CheckedTag[1] = ( Prefs.Flags & TO_LOWER ) ? FALSE : TRUE;
  187.     GT_SetGadgetAttrsA( Gadgets[ GD_ToLower ], Wnd,
  188.             NULL, (struct TagItem *)CheckedTag );
  189.  
  190.     /*  ...or return TRUE not to call the gadget function  */
  191.     return ToLowerClicked( Wnd, Gadgets, IE, Msg );
  192. }
  193.  
  194. BOOL SmartStrKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  195. {
  196.     /*  Routine when "_Smart String"'s activation key is pressed  */
  197.     CheckedTag[1] = ( Prefs.Flags & SMART_STR ) ? FALSE : TRUE;
  198.     GT_SetGadgetAttrsA( Gadgets[ GD_SmartStr ], Wnd,
  199.             NULL, (struct TagItem *)CheckedTag );
  200.  
  201.     /*  ...or return TRUE not to call the gadget function  */
  202.     return SmartStrClicked( Wnd, Gadgets, IE, Msg );
  203. }
  204.  
  205. BOOL NewTmpKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  206. {
  207.     CheckedTag[1] = ( Prefs.Flags & ONLY_NEW_TMP ) ? FALSE : TRUE;
  208.     GT_SetGadgetAttrsA( Gadgets[ GD_NewTmp ], Wnd,
  209.             NULL, (struct TagItem *)CheckedTag );
  210.  
  211.     return NewTmpClicked( Wnd, Gadgets, IE, Msg );
  212. }
  213.  
  214. BOOL CatCompKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  215. {
  216.     CheckedTag[1] = ( Prefs.MoreFlags & USE_CATCOMP ) ? FALSE : TRUE;
  217.     GT_SetGadgetAttrsA( Gadgets[ GD_CatComp ], Wnd,
  218.             NULL, (struct TagItem *)CheckedTag );
  219.  
  220.     return CatCompClicked( Wnd, Gadgets, IE, Msg );
  221. }
  222.  
  223. BOOL NoKPKeyPressed( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  224. {
  225.     CheckedTag[1] = ( Prefs.MoreFlags & NO_BUTTON_KP ) ? FALSE : TRUE;
  226.     GT_SetGadgetAttrsA( Gadgets[ GD_NoKP ], Wnd,
  227.             NULL, (struct TagItem *)CheckedTag );
  228.  
  229.     return NoKPClicked( Wnd, Gadgets, IE, Msg );
  230. }
  231.  
  232. BOOL MsgClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  233. {
  234.     /*  Routine when "Intui_Message " is clicked  */
  235.  
  236.     Prefs.Flags ^= INTUIMSG;
  237.  
  238.     return( 0 );
  239. }
  240.  
  241. BOOL ClickClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  242. {
  243.     /*  Routine when "Clicked _Ptr  " is clicked  */
  244.  
  245.     Prefs.Flags ^= CLICKED;
  246.  
  247.     return( 0 );
  248. }
  249.  
  250. BOOL UseClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  251. {
  252.     /*  Routine when "_Ok" is clicked  */
  253.  
  254.     strcpy( Prefs.ChipString,   GetString( Gadgets[ GD_Chip     ] ));
  255.     strcpy( Prefs.HeadersFile,  GetString( Gadgets[ GD_Headers  ] ));
  256.     strcpy( Prefs.HookDef,      GetString( Gadgets[ GD_Hook     ] ));
  257.     strcpy( Prefs.RegisterDef,  GetString( Gadgets[ GD_Reg      ] ));
  258.  
  259.     return( 1 );
  260. }
  261.  
  262. BOOL SaveClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  263. {
  264.     /*  Routine when "_Save" is clicked  */
  265.  
  266.     UseClicked( Wnd, Gadgets, IE, Msg );
  267.  
  268.     SavePrefs();
  269.  
  270.     return( 1 );
  271. }
  272.  
  273. BOOL CancClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  274. {
  275.     Prefs.Flags     = Back;
  276.     Prefs.MoreFlags = MoreBack;
  277.  
  278.     return( 1 );
  279. }
  280.  
  281. BOOL HandlerClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  282. {
  283.     /*  Routine when "IDCMP _Handler" is clicked  */
  284.  
  285.     Prefs.Flags ^= IDCMP_HANDLER;
  286.  
  287.     if( Prefs.Flags & IDCMP_HANDLER ) {
  288.  
  289.         Prefs.Flags &= ~( CLICKED | INTUIMSG );
  290.  
  291.         MsgKeyPressed( Wnd, Gadgets, IE, Msg );
  292.         ClickKeyPressed( Wnd, Gadgets, IE, Msg );
  293.     }
  294.  
  295.     return( 0 );
  296. }
  297.  
  298. BOOL KeyHandlerClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  299. {
  300.     /*  Routine when "_Key Handler  " is clicked  */
  301.  
  302.     Prefs.Flags ^= KEY_HANDLER;
  303.  
  304.     if( Prefs.Flags & KEY_HANDLER ) {
  305.  
  306.         Prefs.Flags &= ~( CLICKED | INTUIMSG );
  307.  
  308.         MsgKeyPressed( Wnd, Gadgets, IE, Msg );
  309.         ClickKeyPressed( Wnd, Gadgets, IE, Msg );
  310.     }
  311.  
  312.     return( 0 );
  313. }
  314.  
  315. BOOL TemplateClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  316. {
  317.     /*  Routine when "_Template     " is clicked  */
  318.  
  319.     Prefs.Flags ^= GEN_TEMPLATE;
  320.  
  321.     return( 0 );
  322. }
  323.  
  324. BOOL ToLowerClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  325. {
  326.     /*  Routine when "To Lo_wer     " is clicked  */
  327.  
  328.     Prefs.Flags ^= TO_LOWER;
  329.  
  330.     return( 0 );
  331. }
  332.  
  333. BOOL ChipClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  334. {
  335.     /*  Routine when "_UWORD chip:" is clicked  */
  336.  
  337.     return( 0 );
  338. }
  339.  
  340. BOOL HookClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  341. {
  342.     /*  Routine when "Hook_:" is clicked  */
  343.  
  344.     return( 0 );
  345. }
  346.  
  347. BOOL RegClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  348. {
  349.     /*  Routine when "Re_gister:" is clicked  */
  350.  
  351.     return( 0 );
  352. }
  353.  
  354. BOOL HeadersClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  355. {
  356.     /*  Routine when "H_eaders:" is clicked  */
  357.  
  358.     return( 0 );
  359. }
  360.  
  361. BOOL SmartStrClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  362. {
  363.     /*  Routine when "_Smart String" is clicked  */
  364.  
  365.     Prefs.Flags ^= SMART_STR;
  366.  
  367.     return( 0 );
  368. }
  369.  
  370. BOOL NewTmpClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  371. {
  372.     Prefs.Flags ^= ONLY_NEW_TMP;
  373.  
  374.     return( 0 );
  375. }
  376.  
  377. BOOL CatCompClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  378. {
  379.     Prefs.MoreFlags ^= USE_CATCOMP;
  380.  
  381.     return( 0 );
  382. }
  383.  
  384. BOOL NoKPClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  385. {
  386.     Prefs.MoreFlags ^= NO_BUTTON_KP;
  387.  
  388.     return( 0 );
  389. }
  390.  
  391. BOOL GetHClicked( struct Window *Wnd, struct Gadget *Gadgets[], struct IE_Data *IE, struct IntuiMessage *Msg )
  392. {
  393.     TEXT    File[ 256 ];
  394.  
  395.     strcpy( File, GetString( Gadgets[ GD_Headers ] ));
  396.  
  397.     if( GetFile( IE, "Select the headers file...", "#?", File )) {
  398.  
  399.     GT_SetGadgetAttrs( Gadgets[ GD_Headers ], Wnd, NULL,
  400.                 GTST_String, File, TAG_END );
  401.     }
  402.  
  403.     return( 0 );
  404. }
  405. ///
  406.  
  407. /// SavePrefs
  408. void SavePrefs( void )
  409. {
  410.     BPTR    file;
  411.  
  412.     if( file = Open( PrefsFile, MODE_NEWFILE )) {
  413.  
  414.     Write( file, &Prefs, sizeof( struct CPrefs ));
  415.  
  416.     Close( file );
  417.     }
  418. }
  419. ///
  420. /// LoadPrefs
  421. void LoadPrefs( void )
  422. {
  423.     BPTR    file;
  424.  
  425.     if( file = Open( PrefsFile, MODE_OLDFILE )) {
  426.  
  427.     Read( file, &Prefs, sizeof( struct CPrefs ));
  428.  
  429.     Close( file );
  430.  
  431.     PrefsOK = TRUE; /* there's no need to read the old prefs from
  432.                IE's data structure                        */
  433.     }
  434. }
  435. ///
  436.  
  437. /// GetFile
  438. BOOL GetFile( struct IE_Data *IE, STRPTR title, STRPTR pattern, STRPTR buffer )
  439. {
  440.     struct FileRequester   *req;
  441.     BOOL                    ok = FALSE;
  442.     static TEXT             initial_file[] = "",
  443.                 initial_drawer[] = "";
  444.  
  445.     if( AslBase = OpenLibrary( "asl.library", 38 )) {
  446.  
  447.     if( req = AllocAslRequest( ASL_FileRequest, NULL )) {
  448.  
  449.         if( ok = AslRequestTags( req, ASLFR_DoPatterns,     TRUE,
  450.                      ASLFR_InitialHeight,  IE->ScreenData->Screen->Height - 40,
  451.                      ASLFR_TitleText,      title,
  452.                      ASLFR_InitialFile,    initial_file,
  453.                      ASLFR_InitialDrawer,  initial_drawer,
  454.                      ASLFR_InitialPattern, pattern,
  455.                      ASLFR_Screen,         IE->ScreenData->Screen,
  456.                      TAG_DONE )) {
  457.  
  458.         strcpy( initial_file,   req->fr_File   );
  459.         strcpy( initial_drawer, req->fr_Drawer );
  460.  
  461.         strcpy( buffer, req->fr_Drawer );
  462.  
  463.         AddPart( buffer, req->fr_File, 256 );
  464.         }
  465.  
  466.         FreeAslRequest( req );
  467.  
  468.     } else
  469.         ( *IE->Functions->Status )( "Cannot open the ASL requester!", TRUE, 0 );
  470.  
  471.     CloseLibrary( AslBase );
  472.  
  473.     AslBase = NULL;
  474.  
  475.     } else
  476.     ( *IE->Functions->Status )( "Cannot open asl.library v38+!", TRUE, 0 );
  477.  
  478.     return( ok );
  479. }
  480. ///
  481.  
  482.